﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;
using System.Text;

public partial class Controls_Activity_StockOutList :BaseUserControl
{
    private SqlCodeConfig codeConfig;
    private string TableName="";
    private string Key="";
    string cmdText = "SELECT T.*,A.CodeName AS StockOutTypeName,TU.UserName as UserName,ORG.Subject as OrgName FROM {0} T LEFT JOIN T_User TU on TU.UserId=T.loginName LEFT JOIN T_OrgPool ORG on ORG.OrgId=T.Department LEFT JOIN " + SqlCodeConfig.TableName + " A ON A.CodeValue=T.StockOutType AND A.Category='" + PMSCodeCategory.PMS_CKLX + "' where 1=1 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "IC_StockOut";
        this.Key = "StockOutID";
        if (!IsPostBack)
        { 
            string sql = string.Format(cmdText + " ORDER BY T.CREATETIME DESC", this.TableName);
            FillGirdView(0, sql);
            InitLoadData();
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
    private void InitLoadData()
    {
        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_CKLX);

        S_StockOutType.DataSource = ds.Tables[0];
        S_StockOutType.DataTextField = "CodeName";
        S_StockOutType.DataValueField = "CodeValue";
        S_StockOutType.DataBind();
 
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder sql = new StringBuilder(string.Format(cmdText, this.TableName));
        if (!string.IsNullOrEmpty(S_StockOutCode.Text))
        {
            sql.Append(" AND T.StockOutCode like '%").Append(S_StockOutCode.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_StockOutDate.Text))
        {
            sql.Append(" AND T.StockOutDate = '").Append(S_StockOutDate.Text).Append("'");
        }
        if (!string.IsNullOrEmpty(S_StockOutType.SelectedValue))
        {
            sql.Append(" AND T.StockOutType = '").Append(S_StockOutType.SelectedValue).Append("'");
        }
        sql.Append(" ORDER BY T.CREATETIME DESC");
        FillGirdView(0, sql.ToString());
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    private string ParseDateToShort(string s)
    {
        if (string.IsNullOrEmpty(s)) return string.Empty;
        DateTime dt = DateTime.Now;
        if (DateTime.TryParse(s, out dt))
        {
            return dt.ToString("yyyy-MM-dd");
        }
        return string.Empty;
    }
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].Text = this.ParseDateToShort(e.Row.Cells[2].Text);
            e.Row.Cells[6].Text = this.ParseDateToShort(e.Row.Cells[6].Text);
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }
}
